package usercenter

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"navi-mumbai/common/gensql"
	"time"
)

var _ BannerConfigModel = (*customBannerConfigModel)(nil)

type (
	// BannerConfigModel is an interface to be customized, add more methods here,
	// and implement the added methods in customBannerConfigModel.
	BannerConfigModel interface {
		bannerConfigModel
		Find(ctx context.Context, state int64, pageNo, pageSize int64) ([]*BannerConfig, error)
		Find2(ctx context.Context, state []int64, pageNo, pageSize int64) ([]*BannerConfig, error)
		Count(ctx context.Context, state int64) (int64, error)
	}

	customBannerConfigModel struct {
		*defaultBannerConfigModel
		logger logx.Logger
		genSql *gensql.GenSql
	}
)

// NewBannerConfigModel returns a model for the database table.
func NewBannerConfigModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) BannerConfigModel {
	return &customBannerConfigModel{
		defaultBannerConfigModel: newBannerConfigModel(conn, c, opts...),
		genSql:                   gensql.NewGenSql(),
	}
}

func (m *customBannerConfigModel) Find(ctx context.Context, state int64, pageNo, pageSize int64) ([]*BannerConfig, error) {
	q := fmt.Sprintf("select %s from %s ", bannerConfigRows, m.table)
	m.genSql.InitSql(q)
	// 修改sql
	if state != 0 {
		m.genSql = m.genSql.Where("state = ?", state)
	}
	now := time.Now()
	query, args, err := m.genSql.Where("start_time <= ?", &now).Where("end_time >= ?", &now).OrderBy("banner_order ASC").Limit(uint64(pageSize)).Offset(uint64((pageNo - 1) * pageSize)).ToSql()
	if err != nil {
		return nil, err
	}

	if m.logger == nil {
		m.logger = logx.WithContext(ctx)
	}
	//m.logger.Infof("customBannerConfigModel Find query == %s \n args == %#v", query, args)

	var list []*BannerConfig
	err = m.QueryRowsNoCacheCtx(ctx, &list, query, args...)
	if err != nil {
		m.logger.Errorf("customBannerConfigModel Find failed err %s ", err.Error())
		return nil, err
	}
	return list, nil
}

func (m *customBannerConfigModel) Find2(ctx context.Context, state []int64, pageNo, pageSize int64) ([]*BannerConfig, error) {
	q := fmt.Sprintf("select %s from %s ", bannerConfigRows, m.table)
	m.genSql.InitSql(q)

	if len(state) != 0 {
		if len(state) > 1 {
			m.genSql = m.genSql.Where("state  = ?", state)
		} else {
			m.genSql = m.genSql.Where("state = ?", state[0])
		}
	}
	query, args, err := m.genSql.OrderBy("banner_order ASC").Limit(uint64(pageSize)).Offset(uint64((pageNo - 1) * pageSize)).ToSql()
	if err != nil {
		return nil, err
	}

	var list []*BannerConfig
	err = m.QueryRowsNoCacheCtx(ctx, &list, query, args...)
	if err != nil {
		return nil, err
	}
	return list, nil
}

func (m *customBannerConfigModel) Count(ctx context.Context, state int64) (int64, error) {
	q := fmt.Sprintf("select count(id) from %s ", m.table)
	m.genSql.InitSql(q)

	if state != 0 {
		m.genSql = m.genSql.Where("state in = ?", state)
	}

	query, args, err := m.genSql.ToSql()
	if err != nil {
		return 0, err
	}
	var cnt int64
	err = m.QueryRowNoCacheCtx(ctx, &cnt, query, args...)
	if err != nil {
		return 0, err
	}
	return cnt, nil
}
